package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.CategoryTreeVo;
import com.atguigu.gmall.search.entity.SearchAttr;
import com.google.common.collect.Lists;
import java.util.Date;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.feign.search.SearchFeignClient;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import com.atguigu.gmall.product.vo.SkuInfoVo;
import com.atguigu.gmall.search.entity.Goods;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.messages.Item;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements SkuInfoService {

    @Autowired
    SkuAttrValueService skuAttrValueService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SearchFeignClient searchFeignClient;

    @Autowired
    BaseTrademarkService baseTrademarkService;

    @Autowired
    BaseCategoryService baseCategoryService;

    @Transactional
    @Override
    public void saveSkuInfo(SkuInfoVo skuInfoVo) {
        //1 添加skuInfo
        SkuInfo skuInfo = new SkuInfo();
        BeanUtils.copyProperties(skuInfoVo,skuInfo);
        save(skuInfo);

        Long skuId = skuInfo.getId();
        //2 添加skuAttrValue
        List<SkuAttrValue> skuAttrValueList = skuInfoVo.getSkuAttrValueList().stream().map(item -> {
            SkuAttrValue skuAttrValue = new SkuAttrValue();
            BeanUtils.copyProperties(item, skuAttrValue);
            skuAttrValue.setSkuId(skuId);
            return skuAttrValue;
        }).collect(Collectors.toList());
        skuAttrValueService.saveBatch(skuAttrValueList);

        //3 添加skuSaleAttrValue
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfoVo.getSkuSaleAttrValueList().stream().map(item -> {
            SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
            skuSaleAttrValue.setSkuId(skuId);
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
            skuSaleAttrValue.setSaleAttrValueId(item.getSaleAttrValueId());
            return skuSaleAttrValue;
        }).collect(Collectors.toList());
        skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);

        //4 添加image
        List<SkuImage> skuImageList = skuInfoVo.getSkuImageList().stream().map(item -> {
            SkuImage skuImage = new SkuImage();
            BeanUtils.copyProperties(item, skuImage);
            skuImage.setSkuId(skuId);
            return skuImage;
        }).collect(Collectors.toList());
        skuImageService.saveBatch(skuImageList);

        //同步bitmap
        redisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP,skuId,true);
    }

//    @Override
//    public void onSale(Long skuId) {
//        SkuInfo skuInfo = getById(skuId);
//        skuInfo.setIsSale((byte) 1);
//        updateById(skuInfo);
//    }
//
//    @Override
//    public void cancelSale(Long skuId) {
//        SkuInfo skuInfo = getById(skuId);
//        skuInfo.setIsSale((byte) 0);
//        updateById(skuInfo);
//    }

    @Override
    public void onSale(Long skuId) {
        //1 修改数据库
        boolean update = lambdaUpdate().set(SkuInfo::getIsSale, 1)
                .eq(SkuInfo::getId, skuId).update();

        //2 保存到es
        if (update){
            Goods goods = prepareGoods(skuId);
            searchFeignClient.up(goods);
        }

    }

    @Override
    public void cancelSale(Long skuId) {
        //1 修改数据库
        boolean update = lambdaUpdate().set(SkuInfo::getIsSale, 0)
                .eq(SkuInfo::getId, skuId).update();

        //2 远程下架
        if (update){
            searchFeignClient.down(skuId);
        }
    }

    private Goods prepareGoods(Long skuId){
        SkuInfo skuInfo = getById(skuId);
        Goods goods = new Goods();
        goods.setId(skuId);
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date());

        BaseTrademark trademark = baseTrademarkService.getById(skuInfo.getTmId());
        goods.setTmId(trademark.getId());
        goods.setTmName(trademark.getTmName());
        goods.setTmLogoUrl(trademark.getLogoUrl());

        CategoryTreeVo categoryTreeVo = baseCategoryService.getCategoryTreeWithC3Id(skuInfo.getCategory3Id());
        goods.setCategory1Id(categoryTreeVo.getCategoryId());
        goods.setCategory1Name(categoryTreeVo.getCategoryName());

        CategoryTreeVo child = categoryTreeVo.getCategoryChild().get(0);
        goods.setCategory2Id(child.getCategoryId());
        goods.setCategory2Name(child.getCategoryName());

        CategoryTreeVo child2 = child.getCategoryChild().get(0);
        goods.setCategory3Id(child2.getCategoryId());
        goods.setCategory3Name(child2.getCategoryName());
        goods.setHotScore(0L);

        List<SearchAttr> attrs = skuAttrValueService.getSkuAttrsAndValue(skuId);
        goods.setAttrs(attrs);
        return goods;
    }
}




